package com.example.demo01.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.demo01.R;

import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment01#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment01 extends Fragment {


    TextView tv_fragment_text;
    Button btn_edit_text;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test01, container, false);

        initView(view);

        return view;
    }

    //. 初始化视图
    private void initView(View v) {
        btn_edit_text = v.findViewById(R.id.btn_edit_text);
        tv_fragment_text = v.findViewById(R.id.tv_fragment_text);

        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        btn_edit_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = (String) tv_fragment_text.getText();
                if (Objects.equals(text, "这是 fragment01")) {
                    tv_fragment_text.setText("这是修改后的文本");
                } else {
                    tv_fragment_text.setText("这是 fragment01");
                }
            }
        });
    }
}